/// 浏览历史记录模型 - 用于本地SQLite存储
/// Browse History Model - For local SQLite storage
class BrowseHistory {
  final int? id;
  final String wikiId;
  final String wikiName;
  final String wikiUrl;
  final String title;
  final String description;
  final String iconUrl;
  final int visitCount;  // 访问次数 Visit count
  final DateTime createdAt;
  final DateTime lastVisitAt;

  BrowseHistory({
    this.id,
    required this.wikiId,
    required this.wikiName,
    required this.wikiUrl,
    required this.title,
    this.description = '',
    this.iconUrl = '',
    this.visitCount = 1,
    required this.createdAt,
    required this.lastVisitAt,
  });

  factory BrowseHistory.fromJson(Map<String, dynamic> json) {
    return BrowseHistory(
      id: json['id'] as int?,
      wikiId: json['wikiId'] ?? '',
      wikiName: json['wikiName'] ?? '',
      wikiUrl: json['wikiUrl'] ?? '',
      title: json['title'] ?? '',
      description: json['description'] ?? '',
      iconUrl: json['iconUrl'] ?? '',
      visitCount: json['visitCount'] ?? 1,
      createdAt: DateTime.parse(json['createdAt'] ?? DateTime.now().toIso8601String()),
      lastVisitAt: DateTime.parse(json['lastVisitAt'] ?? DateTime.now().toIso8601String()),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'wikiId': wikiId,
      'wikiName': wikiName,
      'wikiUrl': wikiUrl,
      'title': title,
      'description': description,
      'iconUrl': iconUrl,
      'visitCount': visitCount,
      'createdAt': createdAt.toIso8601String(),
      'lastVisitAt': lastVisitAt.toIso8601String(),
    };
  }

  /// 转换为数据库映射 Convert to database map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'wiki_id': wikiId,
      'wiki_name': wikiName,
      'wiki_url': wikiUrl,
      'title': title,
      'description': description,
      'icon_url': iconUrl,
      'visit_count': visitCount,
      'created_at': createdAt.millisecondsSinceEpoch,
      'last_visit_at': lastVisitAt.millisecondsSinceEpoch,
    };
  }

  /// 从数据库映射创建 Create from database map
  factory BrowseHistory.fromMap(Map<String, dynamic> map) {
    return BrowseHistory(
      id: map['id'] as int?,
      wikiId: map['wiki_id'] ?? '',
      wikiName: map['wiki_name'] ?? '',
      wikiUrl: map['wiki_url'] ?? '',
      title: map['title'] ?? '',
      description: map['description'] ?? '',
      iconUrl: map['icon_url'] ?? '',
      visitCount: map['visit_count'] ?? 1,
      createdAt: DateTime.fromMillisecondsSinceEpoch(map['created_at'] ?? 0),
      lastVisitAt: DateTime.fromMillisecondsSinceEpoch(map['last_visit_at'] ?? 0),
    );
  }

  BrowseHistory copyWith({
    int? id,
    String? wikiId,
    String? wikiName,
    String? wikiUrl,
    String? title,
    String? description,
    String? iconUrl,
    int? visitCount,
    DateTime? createdAt,
    DateTime? lastVisitAt,
  }) {
    return BrowseHistory(
      id: id ?? this.id,
      wikiId: wikiId ?? this.wikiId,
      wikiName: wikiName ?? this.wikiName,
      wikiUrl: wikiUrl ?? this.wikiUrl,
      title: title ?? this.title,
      description: description ?? this.description,
      iconUrl: iconUrl ?? this.iconUrl,
      visitCount: visitCount ?? this.visitCount,
      createdAt: createdAt ?? this.createdAt,
      lastVisitAt: lastVisitAt ?? this.lastVisitAt,
    );
  }

  /// 增加访问次数 Increment visit count
  BrowseHistory incrementVisit() {
    return copyWith(
      visitCount: visitCount + 1,
      lastVisitAt: DateTime.now(),
    );
  }

  @override
  String toString() {
    return 'BrowseHistory{id: $id, wikiId: $wikiId, title: $title, visitCount: $visitCount, lastVisitAt: $lastVisitAt}';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is BrowseHistory &&
          runtimeType == other.runtimeType &&
          wikiId == other.wikiId &&
          wikiUrl == other.wikiUrl;

  @override
  int get hashCode => wikiId.hashCode ^ wikiUrl.hashCode;
}